package com.itany.corejava.day14;

import java.io.*;

public class SerializableUtil {

    //对象写入内存
    public static byte[] encoderObject(Serializable obj){

        byte[] b=null;

        try(ByteArrayOutputStream baos=new ByteArrayOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(baos)){

            oos.writeObject(obj);

            b=baos.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }


        return b;
    }

    //内存读出对象
    public static Serializable decoderObject(byte[] b){

        Serializable obj=null;

        try(ObjectInputStream ois=new ObjectInputStream(
                new ByteArrayInputStream(b)
        )){

            obj=(Serializable) ois.readObject();

        }catch (Exception e){
            e.printStackTrace();
        }

        return obj;
    }


}
